package fr.inserm.umr915.vcfserver.bio.ucsc.snp;


import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import com.sleepycat.bind.tuple.TupleBinding;

import fr.inserm.umr915.vcfserver.bio.Chromosomes;
import fr.inserm.umr915.vcfserver.bio.Segment;
import fr.inserm.umr915.vcfserver.bio.berkeley.DatabaseManager;
import fr.inserm.umr915.vcfserver.util.ColumnSerializer;
import fr.inserm.umr915.vcfserver.util.NameExtractor;
import fr.inserm.umr915.vcfserver.xml.XmlStreamSerializer;


public class SnpHandler extends DatabaseManager<Snp>
	{
	private Snp.Binding dataBinding=null;
	private Snp.StaxSerializer stax=null;
	private String version;
	private NameExtractor<Snp> nameExtactor=new NameExtractor<Snp>()
		{
		@Override
		public String getName(Snp o)
			{
			return o==null?null:o.getName();
			}
		};
	private ColumnSerializer<Snp> delimitedOutputHandler=null;
	
	
	
	public SnpHandler(String version,Chromosomes chromosomes)
		{
		setName(version);
		this.version=version;
		this.dataBinding=new Snp.Binding(chromosomes);
		this.stax=new Snp.StaxSerializer(version);
		this.delimitedOutputHandler=new Snp.DelimitedOutputSerializer(version);
		}
	
	
	@Override
	public TupleBinding< Snp> getDataBinding()
		{
		return this.dataBinding;
		}
	
	@Override
	public XmlStreamSerializer<Snp> getXmlStreamSerializer()
		{
		return stax;
		}
	
	@Override
	public NameExtractor<Snp> getNameExtractor() {
		return this.nameExtactor;
		}
	
	@Override
	public ColumnSerializer<Snp> getColumnSerializer()
		{
		return this.delimitedOutputHandler;
		}

	@Override
	public Snp parse(String line) throws IOException
		{
		Snp g= Snp.parse(getDelim().split(line));
		if(!acceptChromosome(g.getChrom())) return null;
		return g;
		}
	@Override
	protected Segment getSegment(Snp o) {
		return new Segment(o.getChrom(),o.getChromStart(),o.getChromEnd());
		}
	@Override
	protected Set<String> getNames(Snp g)
		{
		Set<String> set=new HashSet<String>();
		set.add(g.getName());
		return set;
		}
	}
